/*
 * 文 件 名:  CurveGraphView.java
 * 描    述:  <描述>
 * 修 改 人:  杨强
 * 修改时间:  2013-10-29
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.afd.ozblip.view;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.activity.R;
import com.afd.ozblip.activity.UserSignedActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

/**
 * 
 * @author 杨强
 * @version [版本号, 2013-10-29]
 */
public class SignHistogramGraphView extends View {
	private int mMax = 0;
	private int mMin = -100;
	/** 图表Y轴小刻度个数 */
	private int mYCont = 5;
	/** 图表X轴刻度个数 */
	private int mXCount = 2 * 5;
	Paint mTextPaint;
	/** 图表宽度 */
	private float mWidth;
	/** 图表高度 */
	private float mHeight;
	/** 图表Y轴刻度宽度 */
	private float mCalibrationWidth = 10;
	/** 图表Y轴刻度高度 */
	private float mCalibrationHeight = 20;
	/** 图表Y轴文字最大宽度 */
	private float mTextWidth = 20;
	/** 图表X轴文字高度 */
	private float mTextHeight = 40;
	/** 图表X坐标偏移值 */
	private float mDrawXOffset = 10;
	/** 图表Y坐标偏移值 */
	private float mDrawYOffset = 2;
	/** 图表X轴左侧偏移值 */
	private float xLeftOffset = 15;
	/** 图表X轴左侧偏移值 */
	private float xRightOffset = 15;
	/** 图表原点X坐标 */
	private float originX = 0;
	/** 图表原点Y坐标 */
	private float originY = 0;

	/** 图表图标宽度 */
	private int icoWidth = 0;
	/** 图表图标高度 */
	private int icoHeight = 0;
	private Bitmap mBitmap;
	private List<Map<String, Object>> mListData;
	private Context mContext;
	private Bitmap[] bitArr;

	/**
	 * <默认构造函数>
	 */
	public SignHistogramGraphView(Context context) {
		super(context);
		this.mContext = context;
		init();
	}

	/**
	 * <默认构造函数>
	 */
	public SignHistogramGraphView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		init();
	}

	/**
	 * <默认构造函数>
	 */
	public SignHistogramGraphView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		init();
	}

	@SuppressLint("UseSparseArrays")
	private void init() {
		// 参数从dimen获取
		mCalibrationWidth = getResources().getDimensionPixelSize(
				R.dimen.sign_chart_calibration_width);
		mCalibrationHeight = getResources().getDimensionPixelSize(
				R.dimen.sign_chart_calibration_height);
		mTextWidth = getResources().getDimensionPixelSize(
				R.dimen.sign_chart_text_max_width);
		mTextHeight = getResources().getDimensionPixelSize(
				R.dimen.sign_chart_text_height);
		mDrawXOffset = getResources().getDimensionPixelSize(
				R.dimen.sign_chart_draw_x_offset);
		mDrawYOffset = getResources().getDimensionPixelSize(
				R.dimen.sign_chart_draw_y_offset);
		xLeftOffset = getResources().getDimensionPixelSize(
				R.dimen.sign_chart_x_left_offset);
		xRightOffset = getResources().getDimensionPixelSize(
				R.dimen.sign_chart_x_right_offset);
		// 初始化
		mTextPaint = new Paint();
		mTextPaint.setAntiAlias(true);
		mTextPaint.setTextSize(getResources().getDimension(
				R.dimen.sign_chart_text_size));
		float mMaxWidth = mTextPaint.measureText(String.valueOf(mMax));
		float mMinWidth = mTextPaint.measureText(String.valueOf(mMin));
		// 计算文字最大宽度
		mTextWidth = mMaxWidth > mMinWidth ? mMaxWidth : mMinWidth;
		mListData = new LinkedList<Map<String, Object>>();
		icoWidth = getResources().getDimensionPixelSize(
				R.dimen.sign_chart_ico_width);
		icoHeight = getResources().getDimensionPixelSize(
				R.dimen.sign_chart_ico_height);
		// 初始化图片资源
		bitArr = new Bitmap[7];
		bitArr[0] = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.list_ico2);
		// int width = bitArr[0].getWidth();
		// int height = bitArr[0].getHeight();
		// // 创建操作图片用的matrix对象
		// Matrix matrix = new Matrix();
		// // 缩放图片动作
		// matrix.postScale(((float) 20) / width, ((float) 20) / height);
		bitArr[0] = Bitmap.createScaledBitmap(bitArr[0], icoWidth, icoHeight,
				true);

		bitArr[1] = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.list_ico3);
		bitArr[1] = Bitmap.createScaledBitmap(bitArr[1], icoWidth, icoHeight,
				true);
		bitArr[2] = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.list_ico4);
		bitArr[2] = Bitmap.createScaledBitmap(bitArr[2], icoWidth, icoHeight,
				true);
		bitArr[3] = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.list_ico5);
		bitArr[3] = Bitmap.createScaledBitmap(bitArr[3], icoWidth, icoHeight,
				true);
		bitArr[4] = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.list_ico6);
		bitArr[4] = Bitmap.createScaledBitmap(bitArr[4], icoWidth, icoHeight,
				true);
		bitArr[5] = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.list_ico7);
		bitArr[5] = Bitmap.createScaledBitmap(bitArr[5], icoWidth, icoHeight,
				true);
		bitArr[6] = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.list_ico8);
		bitArr[6] = Bitmap.createScaledBitmap(bitArr[6], icoWidth, icoHeight,
				true);
	}

	public void init(int min, int max, int yCount, int xCount) {
		this.mMax = max;
		this.mMin = min;
		this.mYCont = yCount;
		this.mXCount = xCount;

		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawCoordinate(canvas);
		drawHistogram(canvas);
	}

	public void setData(List<Map<String, Object>> lisData) {
		mListData.clear();
		mListData.addAll(lisData);
		invalidate();
	}

	/**
	 * 绘制坐标
	 * 
	 * @param canvas
	 */
	private void drawCoordinate(Canvas canvas) {

		// 获取宽度
		mWidth = getWidth();
		// 获取高度 减去Y轴偏移值
		mHeight = getHeight() - mDrawYOffset;
		// 设置原点坐标
		originX = mTextWidth + mDrawXOffset;
		originY = mHeight - mTextHeight;
		if (mWidth <= 0) {
			return;
		}
		// 获取bitmap对象
		mBitmap = Bitmap.createBitmap(getWidth(), getHeight(),
				Bitmap.Config.ARGB_8888);

		// 创建一个虚线轮廓
		// 代码中的float数组,必须是偶数长度,且>=2,指定了多少长度的实线之后再画多少长度的空白.
		// 本代码中,绘制长度3的实线,再绘制长度3的空白,再绘制长度3的实线,再绘制长度3的空白,依次重复.3是偏移量
		PathEffect mEffects = new DashPathEffect(new float[] { 3, 3, 3, 3 }, 3);
		// 创建一个画笔对象
		Paint mDrawPaint = new Paint();
		// 设置画笔颜色
		mDrawPaint.setColor(Color.BLACK);
		// 设置画笔为无锯齿
		mDrawPaint.setAntiAlias(true);
		// 设置空心的边框宽度
		mDrawPaint.setStrokeWidth(3);
		// 初始化画布
		Canvas mCanvas = new Canvas(mBitmap);
		// 背景色
		mCanvas.drawColor(Color.TRANSPARENT);

		// 画图表Y轴线
		mCanvas.drawLine(originX, 0, originX, originY, mDrawPaint);
		// 画图表X轴线
		mCanvas.drawLine(originX, originY, mWidth, originY, mDrawPaint);
		// 计算刻度文字间隔
		float mTextOffset = (mMax - mMin) / mYCont;

		// 获取Y轴最小刻度高度
		float mYOffset = (originY - mCalibrationHeight) / (mYCont * 5);

		Paint mEffectsPaint = new Paint(mDrawPaint);
		mEffectsPaint.setPathEffect(mEffects);
		mEffectsPaint.setAlpha(100);

		for (int i = mYCont * 5; i >= 0; i--) {
			// 计算每个刻度起始Y坐标
			float mStartY = originY - i * mYOffset;
			// 画左侧
			if (i % 5 == 0) {
				// 获取刻度值
				String mText = String
						.valueOf((int) (mMin + i / 5 * mTextOffset));
				float mTextW = mTextPaint.measureText(mText);
				// 画文字
				mCanvas.drawText(mText, mTextWidth - mTextW, mStartY
						+ mDrawYOffset, mTextPaint);
				// 画虚线
				// mCanvas.drawLine(mTextWidth + mDrawXOffset, mStartY, mWidth,
				// mStartY, mEffectsPaint);
				// 画刻度点长实线
				mCanvas.drawLine(originX, mStartY, originX + mCalibrationWidth,
						mStartY, mDrawPaint);
			} else {
				// 画刻度点短实线
				// mCanvas.drawLine(originX, mStartY, originX +
				// mCalibrationWidth
				// / 2, mStartY, mDrawPaint);
			}
		}

		canvas.drawBitmap(mBitmap, 0, 0, null);
	}

	/**
	 * 绘制曲线
	 */
	private void drawHistogram(Canvas canvas) {

		// X轴间隔值
		float mXOffset = (mWidth - originX - xLeftOffset - xRightOffset)
				/ mXCount;
		// Y轴刻度值
		float mYOffset = (originY - mCalibrationHeight) / Math.abs(mMax - mMin);
		for (int i = 0, len = mListData.size(); i < len; i++) {
			// 初始化柱状图画笔
			Paint mHistogramPaint = new Paint();
			// 参数设置
			mHistogramPaint.setStrokeWidth(2);
			mHistogramPaint.setAntiAlias(true);
			mHistogramPaint.setStyle(Style.FILL);
			mHistogramPaint.setColor(getResources()
					.getColor(R.color.sign_chart));

			String mTVal = String.valueOf(mListData.get(i).get(
					UserSignedActivity.G_VAL));
			String mBVal = String.valueOf(mListData.get(i).get(
					UserSignedActivity.G_KEY));
			// 文字宽度
			float mBTextWidth = mTextPaint.measureText(mBVal);

			float mStartX = (i * 2) * mXOffset + originX + xLeftOffset;
			float mStartY = mStartX + mXOffset;
			float mLeft = mStartX;
			float mRight = mStartY;
			float mTop = Math.abs((mMax - Integer.valueOf(mTVal)) * mYOffset)
					+ mCalibrationHeight;
			float mBottm = mHeight - mTextHeight
					- mHistogramPaint.getStrokeWidth();

			canvas.drawRect(mLeft, mTop, mRight, mBottm, mHistogramPaint);
			canvas.drawText(mBVal, mStartX
					+ ((mXOffset / 2) - (mBTextWidth / 2)), mHeight, mTextPaint);

			// canvas.drawText(String.valueOf(mTVal), mStartX
			// + ((mXOffset / 2) - (mTTextWidth / 2)), mTop - mTextHeight
			// / 4, mTextPaint);

			canvas.drawBitmap(bitArr[i], mLeft, mTop - icoHeight, mTextPaint);
		}
	}
}
